
import { _decorator, Label } from "cc";
import { BaseButton } from "../../framework/base/BaseButton";
import { BaseUI } from "../../framework/base/BaseUI";
import { ADType } from "../../framework/config/Appcfg";
import { SDKMgr } from "../../framework/manager/SDKMgr";
import { UIMgr } from "../../framework/manager/UIMgr";
import { PlayerMgr } from "../manager/PlayerMgr";
const { ccclass, property } = _decorator;

@ccclass('UIDayGit')
export class UIDayGit extends BaseUI {
    //是否正在游戏
    isGaming: boolean = false;
    callback: Function = null;
    private type: number;
    private titleText: string = ""
    public init(param1?: any, param2?: any, param3?: any): void {
        this.type = param1
        this.titleText = param2
        this.callback = param3
    }

    start() {
        this.initBtns();
        this.initView();
    }

    initView() {
        let isValid = (PlayerMgr.instance.userData.data.dayGifts[0] + (60 * 60 * 24)) * 1000 >= Date.now()
        let isValid1 = (PlayerMgr.instance.userData.data.dayGifts[1] + (60 * 60 * 24)) * 1000 >= Date.now()
        let isValid2 = (PlayerMgr.instance.userData.data.dayGifts[2] + (60 * 60 * 24)) * 1000 >= Date.now()
        console.log("isValid", isValid, isValid1, isValid2, Date.now())
        this.findChilds("item1").getChildByName("btnGetItem").setGray(isValid)
        this.findChilds("item1").getChildByName("btnGetItem").getComponent(BaseButton).interactable = !isValid;
        this.findChilds("item1").getChildByName("btnGetItem").getChildByName("tipText").getComponent(Label).string = isValid ? "已领取" : "领取";

        this.findChilds("item2").getChildByName("btnGetItem").getComponent(BaseButton).interactable = !isValid1;
        this.findChilds("item2").getChildByName("btnGetItem").getChildByName("tipText").getComponent(Label).string = isValid1 ? "已领取" : "领取";
        this.findChilds("item2").getChildByName("btnGetItem").setGray(isValid1)

        this.findChilds("item3").getChildByName("btnVideoGetItem").getComponent(BaseButton).interactable = !isValid2;
        this.findChilds("item3").getChildByName("btnVideoGetItem").getChildByName("tipText").getComponent(Label).string = isValid2 ? "已领取" : "领取";
        this.findChilds("item3").getChildByName("btnVideoGetItem").setGray(isValid2)
    }

    initBtns() {
        this.findChilds("btnCloseView").onClick(this.closeBtn, this)

        this.findChilds("item1").getChildByName("btnGetItem").onClick(this.onGetItem, this, 0, 1)

        this.findChilds("item2").getChildByName("btnGetItem").onClick(this.onGetItem, this, 1, 2)

        this.findChilds("item3").getChildByName("btnVideoGetItem").onClick(this.onGetItemViode, this, 2, 3)
    }
    closeBtn() {
        this.closeUI();
    }
    onGetItemViode(skillId: number, count: number) {
        SDKMgr.getInstance().playVideo((isComp) => {
            if (isComp) {
                PlayerMgr.instance.setSkillCount(count, 2)
                PlayerMgr.instance.userData.data.dayGifts[skillId] = Math.floor(Date.now() / 1000)
                PlayerMgr.instance.userData.saveDataToLocad()
                UIMgr.instance.showTip(`获得刷新道具 + 2`)
                this.initView()
            }
            SDKMgr.getInstance().reportEvent("showad", {
                "adtype": "video",
                "opportunity":  ADType.ADDREITME,
                "passpercent":0,
                "video_complete": isComp + '',
                "is_stage": "false",
                "stage": '每日礼包',
                "level_type":"主页"
            })
        }, ADType.ADDREITME)
    }
    onGetItem(skillId: number, count: number) {
        PlayerMgr.instance.setSkillCount(count, 1)
        PlayerMgr.instance.userData.data.dayGifts[skillId] = Math.floor(Date.now() / 1000)
        PlayerMgr.instance.userData.saveDataToLocad()
        UIMgr.instance.showTip(skillId===0?`获得提示道具 + 1`:`获得敲碎道具 + 1`)
        this.initView()
    }
    btnCloseView() {
        this.closeUI();
    }


    onCloseUI(uid: any, msg: string) {

    }

}
